package com.crawler.parser;

import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.kit.StrKit;

public class OrderPageParser extends ParserAdapter {

	/**
	 * 解析订单详情
	 */
	@Override
	public List<Map<String, Object>> parseListMap(String html) {
		Document doc = Jsoup.parse(html);
		Elements elements = doc.getElementsByClass("tb-void").get(0).getElementsByTag("tbody");
		List<Map<String, Object>> maps = Lists.newArrayList();
		for (Element tbody : elements) {
			String tbodyId = tbody.attr("id");
			if(tbodyId.startsWith("tb-")){
				Map<String, Object> map = Maps.newHashMap();
				String orderCode = tbodyId.substring(3,tbodyId.length());//tb-5549411230|parent-7696572928
				String orderVender = tbody.select("span").get(1).text();//卖主
				if(StrKit.isBlank(orderVender)){
					orderVender = tbody.select("a").get(1).text();
				}
				System.err.println(tbody.text());
				String  trackId = "track"+orderCode;// track5549411230
				Element track = tbody.getElementById(trackId);
				String orderName = track.select("td").first().select("img").attr("title");
				String orderAmount = track.select("td").get(2).text();
				String orderTime = track.select("td").get(3).text();
				String orderStatus = track.select("td").get(4).text();
				String orderAddress = track.select("td").get(5).select("a").first().attr("href");
				
				map.put("orderCode", orderCode);
				map.put("orderName", orderName);
				map.put("orderAmount", orderAmount);
				map.put("orderTime", orderTime);
				map.put("orderStatus", orderStatus);
				map.put("orderAddress", orderAddress);
				map.put("orderVender", orderVender);
				maps.add(map);
			}
		}
		return maps;
	}

	/**
	 * 解析下一页URL
	 */
	@Override
	public String parseText(String html) {
		return super.parseText(html);
	}
	
}
